package com.ynkbny.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynkbny.domain.Contacter;
import com.ynkbny.domain.pojo.EnterpriseGetNameVO;
import com.ynkbny.lgapi.YnBk;
import com.ynkbny.mapper.ContacterMapper;
import com.ynkbny.service.ContacterService;
import com.ynkbny.service.outsideinterface.MyClient;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.poi.ss.formula.functions.T;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 杨亚
 * @version 1.0.0
 * @title DataPushUtil
 * @description <ToDo description class purpose>
 * @create 2023/12/12 11:06
 **/
@Slf4j
@Component
public class DataPushUtil {

    @Resource
    private MyClient myClient;

    public  <T> T pushData(String token , T object, String url) {
        log.info("开始上传数据:{}", object);
        try {
            JSONObject jsonObject = myClient.pushBaseData(url, token, object);
            log.info("上传数据结果：{}" , jsonObject.toJSONString());
            if ("200".equals(jsonObject.getString("responseCode"))){
                String dataIndexValue = jsonObject.getJSONObject("data").getString("dataIndex");
                // 赋值端记录索引
                Class<?> clazz = object.getClass();
                Field dataIndex = clazz.getDeclaredField("dataIndex");
                dataIndex.setAccessible(true);
                dataIndex.set(object, dataIndexValue);
                // 赋值更新时间
                Field updateTime = clazz.getDeclaredField("updateTime");
                updateTime.setAccessible(true);
                updateTime.set(object, new Date());
            }
            throw new Exception(jsonObject.getString("responseMessage"));
        } catch (Exception e) {
            log.error("上传报错",e);
        }
        return object;
    }

}
